package com.sgb.link.utils;

import com.sgb.link.api.CommonNet;
import com.sgb.link.entity.LoginTempEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

/**
 * @author: 杨畔
 * @date: 2021/9/18 17:33
 * @Description: 模块工具类，提供一些模块用方法
 */
public class ModuleHelper {

    private static class Single {
        private static final ModuleHelper INSTANCE = new ModuleHelper();
    }

    public static ModuleHelper getInstance() {
        return Single.INSTANCE;
    }


    /**
     * 登录保存token
     * 单模块使用时登录接口，在模块入口activity或者application中调用，传入固定账号（暂存token用）
     *
     * @param username 用户名
     * @param password 密码
     */
    public void loginAndSaveToken(String username, String password) {
        CommonNet.getInstance().login(username, password).enqueue(new ICallBack<BaseEntity<LoginTempEntity>>() {
            @Override
            public void onSuccess(BaseEntity<LoginTempEntity> data) {
                //保存token
                if (data != null && data.getData() != null) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PC_TOKEN, data.getData().getResourcePCToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, data.getData().getResourcePCToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, data.getData().getResourceAPPToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, data.getData().getToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, data.getData().getPhone());
                }
            }
        });
    }
}
